package server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


public class ServerListener {

	private boolean connection;
	private int port;
	
	public ServerListener(int port){
		super();
		this.port = port;
	}
	
	/*getters and setters */
	public int getPort(){
		return this.port;
	}
	
	public void setPort(int port){
		this.port = port;
	}
	
	/*stop listening*/
	public void stopListening(){
		this.connection = false;
	}
	
	
	/*listening function */
	public void listen() throws IOException{
			
			ServerSocket serverSocket = null;
			Socket clientSocket = null;
		
			try{
				
				serverSocket = new ServerSocket(this.port);
				this.connection = true;
			
				System.out.println("Server listening on " + port);
			
				while(this.connection){
					clientSocket = serverSocket.accept();
				
					MastermindThread thread = new MastermindThread(clientSocket);
					thread.start();
				}
			}
			catch(IOException e){
				throw new IOException(e.getMessage());
			}
			
			
		}
		
	}
